//
//  MerchantBuyView.m
//  BigJoy
//
//  Created by Rowling on 2017/7/13.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "MerchantBuyView.h"
#import "MerchantColorChoseView.h"

@interface MerchantBuyView()
@property (weak, nonatomic) IBOutlet UIView *contenView;
@property (nonatomic,assign) NSInteger upCount;
@property (nonatomic,assign) NSInteger downCount;
@property (nonatomic,strong) NSMutableArray <UIButton *>*upArray;
@property (nonatomic,strong) NSMutableArray <UIButton *>*downArray;
@end
@implementation MerchantBuyView

-(NSMutableArray <UIButton *>*)upArray {
    if (_upArray == nil) {
        _upArray = [[NSMutableArray alloc]init];
    }
    return _upArray;
}

-(NSMutableArray<UIButton *> *)downArray {
    if (_downArray == nil) {
        _downArray = [[NSMutableArray alloc]init];
    }
    return _downArray;
}

-(void)awakeFromNib {
    [super awakeFromNib];
    self.upCount = 10;
    self.downCount = 10;
    //btn 之间的空隙
    CGFloat kongxi = (SCREEN_WIDTH - 22 - 50 - 4 * 64) / 3 +64;
    CGFloat shuKongxi = 18 + 30; // 竖直的空隙
    UILabel * colorLable = [XMSuperHelper XMLableWithColor:[UIColor colorForHex:@"666666"] Title:@"颜色:" FontSize:13];
    colorLable.x = 12;
    colorLable.centerY = 15;
    [self.contenView addSubview:colorLable];
    for (int i = 0; i < self.upCount; i ++) {
        int row = i / 4;
        int col = i % 4;
        UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(50 + col * kongxi , row * shuKongxi, 64, 30)];
        btn.tag = i + 200;
        [self setCommomStyle:btn];
        [self.contenView addSubview:btn];
        [self.upArray addObject:btn];
    }
    
    CGFloat startY = CGRectGetMaxY([self.upArray lastObject].frame) + 18;
    CGFloat downKongxi = (SCREEN_WIDTH - 22 - 50 - 7 * 30) / 6 + 30 ; // 下面的btn横向直的空隙
    CGFloat downShukongxi = 18 + 30; // 下面的btn竖直空隙
    for (int i = 0; i < self.downCount; i ++) {
        int row = i / 7;
        int col = i % 7;
        UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(50 + col * downKongxi , startY + row * downShukongxi, 30, 30)];
        btn.tag = 100 + i;
        [self.contenView addSubview:btn];
        [self.downArray addObject:btn];
        [self setCommomStyle:btn];
    }
    UILabel * typLable = [XMSuperHelper XMLableWithColor:[UIColor colorForHex:@"666666"] Title:@"尺码:" FontSize:13];
    typLable.x = 12;
    typLable.centerY = 15 + startY;
    [self.contenView addSubview:typLable];
    self.backgroundColor = [UIColor clearColor];
    self.allHeight = 200 + CGRectGetMaxY([self.downArray lastObject].frame);
}

-(void)setCommomStyle:(UIButton *)btn {
//    [XMSuperHelper clipCircleFromeView:btn withRadius:15];
    btn.layer.cornerRadius = 15;
    [btn setTitleColor:[UIColor colorForHex:@"666666"] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitle:@"白色" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    [btn layoutSubviews];
    btn.layer.shadowOffset = CGSizeMake(0, 3);
    btn.layer.shadowColor = XMColor(150, 150, 150).CGColor;
    btn.layer.shadowOpacity = 0.2;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)btnClick:(UIButton *)sender {
    [self fanxuan:sender];
    sender.selected = YES;
    sender.backgroundColor = XMColor(245, 70, 70);
}

-(void)fanxuan:(UIButton *)sender {
    NSInteger tag = sender.tag - 100;
    if (tag > 99) {
        // 上面的按钮
        for (UIButton * btn in self.upArray) {
            btn.selected = NO;
            btn.backgroundColor = [UIColor whiteColor];
        }
    }else if(tag <= 99){
        //下面的按钮
        for (UIButton * btn  in self.downArray) {
            btn.selected = NO;
            btn.backgroundColor = [UIColor whiteColor];
        }
    }
}



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
}

+(instancetype)fastLogin {
    return FastLogin;
}

@end
